package com.k.Exception;
/**
 * 异常的定义
 * 根据自己的需求定义一个望文生义的异常
 * 提供必要的构造方法为Message进行赋值
 * */
public class TestCustomException {
    public static void main(String[] args) {
        Student student=new Student();
        try {
            student.setAge(222);
        } catch (AgeException e) {
            e.printStackTrace();
        }
        try {
            student.setSex("男");
        } catch (Exception e) {
            System.out.println("性别输入有误"+e.getMessage());
        }
    }
}
//定义受查异常
class AgeException extends Exception{
    public AgeException() {
        super();
    }
    public AgeException(String message) {
        super(message);
    }
}
//定义运行异常
class SetInputException extends RuntimeException{
    public SetInputException() {
    }

    public SetInputException(String message) {
        super(message);
    }
}
class Student{
    private int age;
    private String sex;

    public int getAge() {
        return age;
    }

    /**
     * 方法throws所写的异常与方法内部异常一致
     * */
    public void setAge(int age) throws AgeException{
        //年龄在0~200岁之间
        if ( age>0 && age<200 ){
            this.age = age;
        }else{
            throw new AgeException("年龄在0~200岁之间");
        }
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        //性别只能在男或女之间进行选择
        if (sex.equals("男")|| sex.equals("女")){
            this.sex = sex;
        }else{
            throw new SetInputException("性别只能在男或女之间进行选择");
        }
    }
}
